package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.Demo;
import com.example.demo.service.DemoService;
import com.example.demo.util.BaseResult;
import com.example.demo.util.DataResult;
import com.example.demo.util.ResultCode;

/**
 * @RestController的意思就是controller里面的方法都以json格式输出
 * @author chun
 * @date 2018-11-26
 */
@RestController
public class DemoController {
	
	private static final Logger LOG = LoggerFactory.getLogger(DemoController.class);
	
	@Autowired
	private DemoService demoService;
	
	@RequestMapping("/demo")
	public String demoStr(){
		LOG.info("log日志开始=====================");
		System.out.println("===========请求demo成功===========");
		LOG.info("log日志结束=====================");
		return "hello world";
	}
	
	@RequestMapping(value= "/demo/add")
	public BaseResult addDemo(String name,int age,String mobile,String address,Date birthday ){
		LOG.info("添加示例实体数据开始=====================");
		Demo demo = new Demo();
		demo.setName(name);
		demo.setAge(age);
		demo.setMobile(mobile);
		demo.setAddress(address);
		demo.setBirthday(birthday);
		demo.setGmtCreate(new Date());
		demo.setGmtModify(new Date());
		int res = demoService.add(demo);
		LOG.info("添加示例实体数据结束=====================生成id : " + res);
		return new BaseResult("1", "添加成功", true);
	}
	
	@RequestMapping(value="/demo/queryOne/{id}",method=RequestMethod.GET)
	public DataResult<Demo> queryOne(@PathVariable("id") long id) {
		Demo demo = demoService.queryOne(id);
		return new DataResult<Demo>(ResultCode.SUCCESS, "查询成功", true, demo);
	}
	
	@RequestMapping(value= "/demo/update")
	public BaseResult modifyDemo(long id,String name,int age,String mobile,String address,Date birthday ){
		LOG.info("修改示例实体数据开始=====================");
		Demo demo = new Demo();
		demo.setId(id);
		demo.setName(name);
		demo.setAge(age);
		demo.setMobile(mobile);
		demo.setAddress(address);
		demo.setBirthday(birthday);
		demo.setGmtCreate(new Date());
		demo.setGmtModify(new Date());
		boolean res = demoService.modify(demo);
		LOG.info("修改示例实体数据结束=====================是否修改成功 : " + res);
		return new BaseResult("1", "修改成功", true);
	}
	
	@RequestMapping(value="/demo/remove/{id}",method=RequestMethod.GET)
	public BaseResult remove(@PathVariable("id") long id) {
		boolean res = demoService.remove(id);
		return new BaseResult(ResultCode.SUCCESS, "查询成功", res);
	}
	
	@RequestMapping(value= "/demo/query")
	public DataResult<Object> queryByDemo(String name, Integer age, String mobile,
			String address, Date birthday, Integer limitPage) {
		LOG.info("添加示例实体数据开始=====================");
		Demo demo = new Demo();
		demo.setName(name);
		demo.setAge(null == age ? 0 : age);
		demo.setMobile(mobile);
		demo.setAddress(address);
		demo.setBirthday(birthday);
		demo.setGmtCreate(new Date());
		demo.setGmtModify(new Date());
		if (0 == limitPage || null == limitPage) {// 如果没有传页码，默认查询第一页,5条数据
			demo.setLimitStart(0);
		} else {
			demo.setLimitStart((limitPage-1)*5);
		}
		demo.setLimitEnd(5); // 默认查询5条
		List<Demo> demos = demoService.querys(demo);
		/**
		 * //总记录数
			int rows=21;  
			//每页显示的记录数
			int pageSize=5;  
			//页数
			int pageSum=(rows-1)/pageSize+1;
		 */
		int counts = demoService.counts(demo);
		LOG.info("添加示例实体数据结束=====================");
		
//		return new DataResult<Object>(ResultCode.SUCCESS, "查询成功", true, demos);
		return new DataResult<Object>(ResultCode.SUCCESS, "查询成功", true, demos, counts);
	}
}
